//
//  SAGlobalSettingsSectionFooterView.m
//  saasRetail
//
//  Created by ice on 2024/8/9.
//

#import "SAGlobalSettingsSectionFooterView.h"

@interface SAGlobalSettingsSectionFooterView ()

@property (nonatomic, strong) UILabel *printTypeLab; //1 小票 2 标签
@property (nonatomic, strong) UIButton *switchBtn;
@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UIButton *receiptBtn;
@property (nonatomic, strong) UIButton *lableBtn;
@property (nonatomic, strong) UILabel *tipLab;

@end

@implementation SAGlobalSettingsSectionFooterView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

-(void)clickedSwitchBtn:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    [kUserDefault setObject:sender.isSelected == YES ? @"1" : @"0" forKey:kPrintOpenKey];
    self.whiteView.hidden = !sender.isSelected;
    self.tipLab.hidden = !sender.isSelected;
}

-(void)clickedSelectBtn:(UIButton *)sender {
    
    if (sender == self.lableBtn) {
        self.lableBtn.selected = YES;
        self.receiptBtn.selected = NO;
        [kUserDefault setObject:@"2" forKey:kPrintTypeKey];
    } else {
        self.lableBtn.selected = NO;
        self.receiptBtn.selected = YES;
        [kUserDefault setObject:@"1" forKey:kPrintTypeKey];
    }
}

#pragma mark - initUI

- (void)initUI {
    
    [self.contentView addSubview:self.printTypeLab];
    [self.contentView addSubview:self.switchBtn];
    [self.contentView addSubview:self.whiteView];
    [self.whiteView addSubview:self.receiptBtn];
    [self.whiteView addSubview:self.lableBtn];
    [self.contentView addSubview:self.tipLab];
    
    [self.printTypeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(25);
        make.height.mas_equalTo(40);
    }];
    
    [self.switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.printTypeLab.mas_right);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(kScaleNum(70));
        make.height.mas_equalTo(kScaleNum(40));
    }];
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    [self.receiptBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(25);
        make.height.mas_equalTo(45);
        make.width.mas_equalTo(150);
    }];
    
    [self.lableBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(175);
        make.height.mas_equalTo(45);
        make.width.mas_equalTo(150);
    }];
    
    [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(90);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
    }];
    
    NSString *printOpen = kPrintOpen;
    if ([printOpen integerValue] == 1) {
        self.switchBtn.selected = YES;
        self.whiteView.hidden = NO;
        self.tipLab.hidden = NO;
    }
    
    NSString *printType = kPrintType;
    if ([printType integerValue] == 2) {
        self.lableBtn.selected = YES;
    } else {
        self.receiptBtn.selected = YES;
    }
}

- (UILabel *)printTypeLab {
    if (!_printTypeLab) {
        _printTypeLab = [[UILabel alloc] init];
        _printTypeLab.font = [UIFont systemFontOfSize:15.0];
        _printTypeLab.textColor = kColor(51, 51, 51, 1);
        _printTypeLab.text = @"双打机模式设置";
    }
    return _printTypeLab;
}

-(UIButton *)switchBtn {
    if (!_switchBtn) {
        _switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_switchBtn setImage:[UIImage imageNamed:@"gray_switch"] forState:UIControlStateNormal];
        [_switchBtn setImage:[UIImage imageNamed:@"green_switch"] forState:UIControlStateSelected];
        [_switchBtn addTarget:self action:@selector(clickedSwitchBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

-(UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [UIView new];
        _whiteView.backgroundColor = [UIColor whiteColor];
        _whiteView.hidden = YES;
    }
    return _whiteView;
}

-(UIButton *)receiptBtn {
    if (!_receiptBtn) {
        _receiptBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_receiptBtn setImage:[UIImage imageNamed:@"shop_blueRound2"] forState:UIControlStateNormal];
        [_receiptBtn setImage:[UIImage imageNamed:@"shop_blueCheck2"] forState:UIControlStateSelected];
        [_receiptBtn setTitle:@"  小票模式" forState:UIControlStateNormal];
        [_receiptBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        _receiptBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        _receiptBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_receiptBtn addTarget:self action:@selector(clickedSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _receiptBtn;
}

-(UIButton *)lableBtn {
    if (!_lableBtn) {
        _lableBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_lableBtn setImage:[UIImage imageNamed:@"shop_blueRound2"] forState:UIControlStateNormal];
        [_lableBtn setImage:[UIImage imageNamed:@"shop_blueCheck2"] forState:UIControlStateSelected];
        [_lableBtn setTitle:@"  标签模式" forState:UIControlStateNormal];
        [_lableBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        _lableBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        _lableBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_lableBtn addTarget:self action:@selector(clickedSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lableBtn;
}

-(UILabel *)tipLab {
    if (!_tipLab) {
        _tipLab = [[UILabel alloc] init];
        _tipLab.font = [UIFont systemFontOfSize:15.0];
        _tipLab.textColor = kRedColor;
        _tipLab.numberOfLines = 0;
        _tipLab.hidden = YES;
        _tipLab.text = @"温馨提示：打印机的模式锁定之后，请确保打印机的模式和所选的模式一致，否则连接失败时，app会关闭该属性，重新启动后可以再次设置该属性";
    }
    return _tipLab;
}



@end
